<?php
/**
 * process.php 处理
 * Jack在该页面中处理自己存储到Inbox中的生词，每次系统给Jack分配一个单词。
 * 系统同时将该单词的释义呈现在页面中，默认的词典为Iciba。
 * 在该页面的下部，系统给出该单词的其他词典的释义以供Jack参考、处理单词。
 */
require_once 'setting.php';
require_once 'smarty.php';

require_once 'functions.php';
require_once 'paging.php';

require_once INCLUDESPATH . 'db/User.php';
require_once INCLUDESPATH . 'db/UserDAO.php';
require_once INCLUDESPATH . 'db/Word.php';
require_once INCLUDESPATH . 'db/WordDAO.php';

# start the session function
if (!isset($_SESSION)) {
	session_start();
}

# << start - ...
# @todo inbox 验证用户是否已经登陆，未登陆的话跳转到对应的页面，已登陆则继续
if (isset($_SESSION['user'])) {
	$user = $_SESSION['user'];
} else {
	jumpToPage('signin.php?redirect=process.php');
}
# >> end - ...

# << start - 处理单词
if (isset($_POST['process'])) {
	# << start - 保存当前用户处理后的单词
	if ($_POST['process'] == 'Save') {
		$id = intval($_POST['id']);
		if ($id < 0) {
			$id = 0;
		}
		$key = isset($_POST['key']) ? addslashes($_POST['key']) : '';
		$ps = isset($_POST['ps']) ? addslashes($_POST['ps']) : '';
		$pron = isset($_POST['pron']) ? addslashes($_POST['pron']) : '';
		$acceptation = isset($_POST['acceptation']) ? addslashes($_POST['acceptation']) : '';
		$worddao = new WordDAO();
		$word = $worddao->retrieveById($id);
		if ($word instanceof Word) {
			$word->setPS($ps);
			$word->setPron($pron);
			$word->setAcceptation($acceptation);
			$word->setStatus('treas');
			# 更新单词的状态
			$worddao->update($word);
		}
		jumpToPage('process.php');
	}
	# >> end - 保存当前用户处理后的单词

	# << start - 将当前用户操作的单词重置为系统默认的值
	elseif ($_POST['process'] == 'Default') {
		jumpToPage('process.php');
	}
	# >> end - 将当前用户操作的单词重置为系统默认的值

	# << start - 删除当前用户操作的单词
	elseif ($_POST['process'] == 'Delete') {
		$id = intval($_POST['id']);
		if ($id < 0) {
			$id = 0;
		}
		if (wordExistWithId($user->getId(), $id)) {
			$worddao = new WordDAO();
			$worddao->delete($id);
			jumpToPage('process.php');
		} else {
			die("An Error occur when delete current word!");
		}
	}
	# >> end - 删除当前用户操作的单词

	# << start - 其他情况
	else {
		jumpToPage('process.php');
	}
	# >> end - 其他情况
} else {
	$word = getNextWord();
	if (is_null($word)) {
		showProcessPage('', '', '', '', array('tips_inbox_no_words' => 'There is no words in the inbox, now!'));
	}
	$results = lookup($word->getKey());

	if (empty($results)) {
		$default = $word;
		$defaultlink = array('key' => 'No Result', 'link' => '#');
	} else {
		if (array_key_exists('iciba', $results)) {
			$default = $results['iciba'];
			$default->setId($word->getId());
			$defaultlink = array('key' => 'Iciba', 'link' => $dictsearchlinks['iciba'] . $word->getKey());
			unset($results['iciba']);
		} else {
			$keys = array_keys($results);
			$defaultlink = array('key' => $keys[0], 'link' => $dictsearchlinks[$keys[0]] . $word->getKey());
			$default = array_shift($results);
			$default->setId($word->getId());
		}
	}
	showProcessPage($word->getKey(), $defaultlink, $default, $results);
}
# >> end - 处理单词

/**
 * 获取下一个需要处理的单词
 */
function getNextWord() {
	global $user;
	$worddao = new WordDAO();
	$properties = array('userid' => $user->getId(), 'status' => 'inbox');
	$words = $worddao->retrieveByPropertiesLimited($properties, 'id', 0, 0, 1);
	if (empty($words)) {
		return null;
	} else {
		return $words[0];
	}
}

/**
 * 52danci系统的Process页面的显示的实现
 *
 * @param unknown_type $word
 * @param unknown_type $defaultlink
 * @param unknown_type $defaultresult
 * @param unknown_type $otherresults
 * @param unknown_type $tipsmsg
 */
function showProcessPage($word, $defaultlink, $defaultresult, $otherresults, $ptipsmsg = '') {
	global $pagetitle, $tipsmsg;
	$smarty = new MySmarty();

	# 页面的标题
	if ($word) {
		$smarty->assign('title', $word . ' - ' . $pagetitle['process']);
	} else {
		$smarty->assign('title', $pagetitle['process']);
	}
	if ($defaultlink) {
		$smarty->assign('defaultlink', $defaultlink);
	}
	if ($defaultresult) {
		$smarty->assign('default', $defaultresult);
	}
	if ($otherresults) {
		$smarty->assign('otherresults', $otherresults);
	}

	# tips messages
	if ($ptipsmsg != '') {
		if (is_array($ptipsmsg)) {
			foreach ($ptipsmsg as $key => $value) {
				$smarty->assign($key, $value);
			}
		}
	}


	# 显示页面
	$smarty->display('process.tpl');
}
?>